package svc

import (
	"database/sql"

	"mall/common/modelx"
	"mall/service/setting/repository"
	"mall/service/setting/rpc/internal/config"

	"github.com/zeromicro/go-zero/core/logx"
)

type ServiceContext struct {
	Config config.Config

	Setting repository.SettingRepository

	DB              *sql.DB
	SysRegion       repository.SysRegionRepository
	MemberRank      repository.MemberRankRepository
	MemberTask      repository.MemberTaskRepository
	IntegralTask    repository.UserIntegralTaskRepository
	OauthProvider   repository.OauthProviderRepository
	FreightTemplate repository.FreightTemplateRepository
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := modelx.NewMysqlConn(c.Mysql.DataSource)
	if err != nil {
		logx.Must(err)
	}

	return &ServiceContext{
		Config:  c,
		Setting: repository.NewSettingRepository(c.Mysql.DataSource, c.CacheRedis),

		DB:              db,
		SysRegion:       repository.NewSysRegionRepository(db, c.CacheRedis),
		MemberRank:      repository.NewMemberRankRepository(db, c.CacheRedis),
		MemberTask:      repository.NewMemberTaskRepository(db, c.CacheRedis),
		IntegralTask:    repository.NewUserIntegralTaskRepository(db, c.CacheRedis),
		OauthProvider:   repository.NewOauthProviderRepository(db, c.CacheRedis),
		FreightTemplate: repository.NewFreightTemplateRepository(db, c.CacheRedis),
	}
}
